package com.kyleswebspace.propertyanalyzer.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.FocusEvent;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import com.kyleswebspace.propertyanalyzer.config.ArcaFont;
import com.kyleswebspace.propertyanalyzer.config.BorderBox;
import com.kyleswebspace.propertyanalyzer.config.ColorBox;
import com.kyleswebspace.propertyanalyzer.model.DataModel;

public class PotentialTaxableIncomePanel extends JPanel implements Updateable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4664543765178226232L;

	private DataModel dm;
	
	private JLabel annualLabel;
	private JLabel projectedCashFlowLabel; 		private JTextField projectedCashFlowField;
	private JLabel year1PPDLabel; 				private JTextField year1PPDField;
	private JLabel depreciationLabel; 			private JTextField depreciationField;
	private JLabel mortgageInsExpenseLabel; 	private JTextField mortgageInsExpenseField;
	private JLabel potentialTaxIncomeLabel; 	private JTextField potentialTaxIncomeField;

	private Color innerPanelColor = ColorBox.innerPanelColor;
	private Font appFont = ArcaFont.appFont;
	
	public PotentialTaxableIncomePanel(DataModel dm) {
		this.dm = dm;
		dm.register(this);
		
		this.setLayout(new GridBagLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder("Potential Taxable Income");
		this.setBorder(title);
		this.setBackground(innerPanelColor);
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.ipadx = 20; //PADDING
		
		//annual label
		annualLabel = new JLabel("Annual ");
		annualLabel.setFont(appFont);
		annualLabel.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridx = 3;
		constraints.gridy = 0;
		constraints.gridwidth = 1;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(annualLabel, constraints);
		
		//est. projected cash flow
		projectedCashFlowLabel = new JLabel("Est. Projected Cash Flow");
		projectedCashFlowLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(projectedCashFlowLabel, constraints);
		projectedCashFlowField = new JTextField(4);
		projectedCashFlowField.setFont(appFont);
		projectedCashFlowField.setHorizontalAlignment(JTextField.RIGHT);
		projectedCashFlowField.addFocusListener(this);
		projectedCashFlowField.setFocusable(false);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(projectedCashFlowField, constraints);
		
		//yr1 PPD
		year1PPDLabel = new JLabel("Yr1 Principle Paydown                                         +");
		year1PPDLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 2;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(year1PPDLabel, constraints);
		year1PPDField = new JTextField(4);
		year1PPDField.addFocusListener(this);
		year1PPDField.setFocusable(false);
		year1PPDField.setFont(appFont);
		year1PPDField.setBorder(BorderBox.fieldBorder);
		year1PPDField.setToolTipText("<html>This is the amount your mortgage<br/>" +
				"principle has been reduced by and<br/>" +
				"even though this is not cash in your<br/>" +
				"hand today it is cash you have<br/>" +
				"received and therefore must be<br/>" +
				"included as part of your taxable<br/>" +
				"income.</html>");
		year1PPDField.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(year1PPDField, constraints);
		
		//depreciation
		depreciationLabel = new JLabel("Depreciation                                                          -");
		depreciationLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 3;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(depreciationLabel, constraints);
		depreciationField = new JTextField(4);
		depreciationField.addFocusListener(this);
		depreciationField.setFocusable(false);
		depreciationField.setFont(appFont);
		depreciationField.setBorder(BorderBox.fieldBorder);
		depreciationField.setToolTipText("<html>The Canadian Government allows you to depreciate<br/>" +
				"up to 4% of the building value.The building value is<br/>" +
				"generally 80% of the purchase price but you and your<br/>" +
				"accountant may determine the building value and land<br/>" +
				"value. CRA will not allow you to depreciate land.<br/>" +
				"Keep in mind that the cumulative depreciation must be<br/>" +
				"paid back upon sale of property, this is called<br/>" +
				"recapture. Therefore this is a tax deferral savings.<br/><br/>" +
				"This depreciation is an option and you may take<br/>" +
				"advantage of all this expense or just a portion or none<br/>" +
				"at all. This is a personal decision and you should<br/>" +
				"speak with your accountant for further advice.</html>");
		depreciationField.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(depreciationField, constraints);
		
		//mortgage insurance expense
		mortgageInsExpenseLabel = new JLabel("Mortgage Ins. Expense                                         -");
		mortgageInsExpenseLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 4;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(mortgageInsExpenseLabel, constraints);
		mortgageInsExpenseField = new JTextField(4);
		mortgageInsExpenseField.addFocusListener(this);
		mortgageInsExpenseField.setFocusable(false);
		mortgageInsExpenseField.setFont(appFont);
		mortgageInsExpenseField.setBorder(BorderBox.fieldBorder);
		mortgageInsExpenseField.setToolTipText("<html>CMHC allows you to add the<br/>" +
				"costs of the CMHC fees to<br/>" +
				"the principle of the mortgage<br/>" +
				"but CRA forces you to<br/>" +
				"expense these fees over the<br/>" +
				"first five years. This actually<br/>" +
				"helps reduce your taxable<br/>" +
				"income for the first five years.</html>");
		mortgageInsExpenseField.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(mortgageInsExpenseField, constraints);
		
		//separator
		constraints.gridx = 0;
		constraints.gridwidth = 4;
		constraints.gridy = 5;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		this.add(new JSeparator(SwingConstants.HORIZONTAL),constraints);
		constraints.fill = GridBagConstraints.NONE;
		
		//potential taxable income
		potentialTaxIncomeLabel = new JLabel("Potential Taxable Income");
		potentialTaxIncomeLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 6;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(potentialTaxIncomeLabel, constraints);
		potentialTaxIncomeField = new JTextField(4);
		potentialTaxIncomeField.addFocusListener(this);
		potentialTaxIncomeField.setFocusable(false);
		potentialTaxIncomeField.setFont(appFont);
		potentialTaxIncomeField.setBorder(BorderBox.fieldBorder);
		potentialTaxIncomeField.setToolTipText("<html>This value is simply a very rough<br/>" +
				"estimate and MUST be verified by your<br/>" + 
				"accountant. Never base your buying<br/>" +
				"decision upon this estimate because<br/>" +
				"the taxable income will depend greatly<br/>" +
				"upon how you operate your business.<br/>" +
				"Do <b>NOT</b> use this figure or any other<br/>" +
				"figures conatined in this spread sheet<br/>" +
				"for tax purposes.</html>");
		potentialTaxIncomeField.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(potentialTaxIncomeField, constraints);
		
		updateFields();
	}

	public void updateFields() {
		this.projectedCashFlowField.setText(dm.getEstProjectedCashFlow());
		this.year1PPDField.setText(dm.getYr1PPD());
		this.depreciationField.setText(dm.getDepreciation());
		this.mortgageInsExpenseField.setText(dm.getMortgageInsExpense());
		this.potentialTaxIncomeField.setText(dm.getPotentialTaxIncome());
	}

	@Override
	public void focusGained(FocusEvent e) {
		if (e.getSource() instanceof JTextField) {
			JTextField focusedField = (JTextField)e.getSource();
			focusedField.selectAll();
		}
		
	}

	@Override
	public void focusLost(FocusEvent e) {
		dm.updateInterface();
	}
}
